{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--2019-04-10 11:48:26--  https://www.dropbox.com/s/xtc56u0npo3ou04/cows_harmonic.t7\n",
      "Resolving www.dropbox.com (www.dropbox.com)... 162.125.70.1, 2620:100:6026:1::a27d:4601\n",
      "Connecting to www.dropbox.com (www.dropbox.com)|162.125.70.1|:443... connected.\n",
      "HTTP request sent, awaiting response... 301 Moved Permanently\n",
      "Location: /s/raw/xtc56u0npo3ou04/cows_harmonic.t7 [following]\n",
      "--2019-04-10 11:48:32--  https://www.dropbox.com/s/raw/xtc56u0npo3ou04/cows_harmonic.t7\n",
      "Reusing existing connection to www.dropbox.com:443.\n",
      "HTTP request sent, awaiting response... 302 Found\n",
      "Location: https://ucbc561ca371bcac5ca3e6e53523.dl.dropboxusercontent.com/cd/0/inline/AesqEAQr5vLJAoRkaZI9do0tI1gFu40IR__YYNN3V21qgfYQvcGbBKFJiWfn9MQX8s_09F8lQzuswt9ae6V0jCrwnfdURmDR3BGOYcMMLLNARw/file# [following]\n",
      "--2019-04-10 11:48:32--  https://ucbc561ca371bcac5ca3e6e53523.dl.dropboxusercontent.com/cd/0/inline/AesqEAQr5vLJAoRkaZI9do0tI1gFu40IR__YYNN3V21qgfYQvcGbBKFJiWfn9MQX8s_09F8lQzuswt9ae6V0jCrwnfdURmDR3BGOYcMMLLNARw/file\n",
      "Resolving ucbc561ca371bcac5ca3e6e53523.dl.dropboxusercontent.com (ucbc561ca371bcac5ca3e6e53523.dl.dropboxusercontent.com)... 162.125.70.6, 2620:100:6026:6::a27d:4606\n",
      "Connecting to ucbc561ca371bcac5ca3e6e53523.dl.dropboxusercontent.com (ucbc561ca371bcac5ca3e6e53523.dl.dropboxusercontent.com)|162.125.70.6|:443... connected.\n",
      "HTTP request sent, awaiting response... 302 FOUND\n",
      "Location: /cd/0/inline2/Aeualb3p3GtlMFb2QiSogt4KWT1L4HvK9EGi5eSLpkcXrORlrpt44pcj4anFjdN7RgNyVaXWN2LVcBm2ITfUEvIxwKolDO36jmWU9HUchxhtweJAKSKcI9QFJPY4Em7Yg_j-cxTvJbSqJDIQ0v8eerL38zVsJ5hBC0P70AG9-mPYM_3PjIbzE_QIxiF3nTfn2EE1fkR-_B6Fw2TlHBf9qAsXCEWgQVb8ZZy3M8UEeLRl-_yMp7FUFxKR-r-97ei_UQGpcPmBq-YKZ04ecSDzL56XAoS3CQY8PE0MCg4gMMFmuVM9rhu2iRTqe6FSCCMq9_ysIP5lBtZmkOXRMqFaVVXu/file [following]\n",
      "--2019-04-10 11:48:33--  https://ucbc561ca371bcac5ca3e6e53523.dl.dropboxusercontent.com/cd/0/inline2/Aeualb3p3GtlMFb2QiSogt4KWT1L4HvK9EGi5eSLpkcXrORlrpt44pcj4anFjdN7RgNyVaXWN2LVcBm2ITfUEvIxwKolDO36jmWU9HUchxhtweJAKSKcI9QFJPY4Em7Yg_j-cxTvJbSqJDIQ0v8eerL38zVsJ5hBC0P70AG9-mPYM_3PjIbzE_QIxiF3nTfn2EE1fkR-_B6Fw2TlHBf9qAsXCEWgQVb8ZZy3M8UEeLRl-_yMp7FUFxKR-r-97ei_UQGpcPmBq-YKZ04ecSDzL56XAoS3CQY8PE0MCg4gMMFmuVM9rhu2iRTqe6FSCCMq9_ysIP5lBtZmkOXRMqFaVVXu/file\n",
      "Reusing existing connection to ucbc561ca371bcac5ca3e6e53523.dl.dropboxusercontent.com:443.\n",
      "HTTP request sent, awaiting response... 200 OK\n",
      "Length: 351476787 (335M) [application/octet-stream]\n",
      "Saving to: ‘cows_harmonic.t7’\n",
      "\n",
      "100%[======================================>] 351 476 787 27,5MB/s   in 12s    \n",
      "\n",
      "2019-04-10 11:48:45 (28,8 MB/s) - ‘cows_harmonic.t7’ saved [351476787/351476787]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!wget https://www.dropbox.com/s/xtc56u0npo3ou04/cows_harmonic.t7 -O cows_harmonic.t7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model loaded\n"
     ]
    }
   ],
   "source": [
    "import torch \n",
    "from torchvision import transforms\n",
    "from harmonic import cluster\n",
    "from models import PSPNet\n",
    "\n",
    "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
    "\n",
    "transform = transforms.Compose(\n",
    "    [transforms.RandomCrop(512),\n",
    "     transforms.ToTensor(),\n",
    "     transforms.Normalize(mean=[0.485, 0.456, 0.406],\n",
    "                          std=[0.229, 0.224, 0.225])])\n",
    "\n",
    "sins = [[23.50503921508789, 0.0, 1.2836638689041138],\n",
    " [-9.183862686157227, 0.0, 0.6610772013664246],\n",
    " [9.274807929992676, 0.0, 0.4241010546684265],\n",
    " [46.33891677856445, 0.0, 0.14550864696502686],\n",
    " [-5.840827465057373, 0.0, 0.052969418466091156],\n",
    " [-39.868263244628906, 0.0, 0.7093945741653442],\n",
    " [0.0, -12.252435684204102, -11.785104751586914],\n",
    " [0.0, 9.418264389038086, -3.8895671367645264],\n",
    " [0.0, 6.086294651031494, 7.134507656097412],\n",
    " [0.0, 80.29950714111328, 7.632736682891846],\n",
    " [0.0, 8.219144821166992, 4.2953972816467285],\n",
    " [0.0, -30.652725219726562, -6.30106258392334]]\n",
    "\n",
    "\n",
    "net = torch.nn.DataParallel(\n",
    "    PSPNet(sins=sins,pretrained=False,extended=True).to(device), [0]).to(device)\n",
    "\n",
    "wdict = torch.load('cows_harmonic.t7')\n",
    "for k in wdict.keys():\n",
    "    if k.endswith('num_batches_tracked'):\n",
    "        wdict.pop(k)\n",
    "net.load_state_dict(wdict)\n",
    "net.eval()\n",
    "print(\"Model loaded\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "img = Image.open(\"images/cow.jpg\").convert('RGB')\n",
    "#estimate the embeddings\n",
    "x = transform(img)\n",
    "emb = net(x.unsqueeze(0).to(device)).detach().cpu().numpy()\n",
    "#cluster embeddings space\n",
    "cc = cluster(emb[0],0.5,minimal_area=1000)\n",
    "\n",
    "f,ax = plt.subplots(nrows=1,ncols=2,figsize=(20,10))\n",
    "ax[0].imshow(x.numpy().transpose(1,2,0)*[0.229, 0.224, 0.225]+[0.485, 0.456, 0.406])\n",
    "ax[1].imshow(cc)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
